#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void countMaxHelper(int matrix[][6], int m, int n, int i, int j, int &cnt)
{
    if (i < 0 || i >= m || j < 0 || j >= n) return;
   
    if (matrix[i][j] != 1) return;
    cnt++;
    matrix[i][j] = 0;
    
    for (int k = 0; k < 4; k++) 
	countMaxHelper(matrix, m, n, i + dx[k], j + dy[k], cnt); 
}

int countMax(int matrix[][6], int m, int n)
{
    int ans = 0; 
    for (int i = 0; i < m; i++) {
	for (int j = 0; j < n; j++) {
	    if (matrix[i][j] == 1)  {
		int cnt = 0;
		countMaxHelper(matrix, m, n, i, j, cnt);
		cout << "I count " << cnt << endl;
		ans = max(ans, cnt);
	    }
	}
    }
    return ans;
}


int main(int argc, char **argv)
{
    srand ( time(NULL) );

    int matrix[6][6];
    for (int i = 0; i < 6; i++) {
	for (int j = 0; j < 6; j++) {
	    matrix[i][j] = rand()%2;
	    cout << matrix[i][j] << " ";
	}
	cout << endl;
    }
    cout <<  countMax(matrix, 6, 6) << endl;
    return 0;
}
